using Lzfy_Refund_Service.Models;
using Lzfy_Refund_Service.Repositories;

namespace Lzfy_Refund_Service.Services
{
    /// <summary>
    /// 退费任务完成情况服务实现
    /// </summary>
    public class RefundTaskCompletionStatusService : IRefundTaskCompletionStatusService
    {
        private readonly IRefundTaskCompletionStatusRepository _repository;
        private readonly ILogger<RefundTaskCompletionStatusService> _logger;

        public RefundTaskCompletionStatusService(
            IRefundTaskCompletionStatusRepository repository,
            ILogger<RefundTaskCompletionStatusService> logger)
        {
            _repository = repository;
            _logger = logger;
        }

        /// <summary>
        /// 获取所有退费任务完成情况
        /// </summary>
        /// <returns>退费任务完成情况列表</returns>
        public async Task<IEnumerable<RefundTaskCompletionStatus>> GetAllAsync()
        {
            try
            {
                _logger.LogInformation("开始获取所有退费任务完成情况");
                var result = await _repository.GetAllAsync();
                _logger.LogInformation("获取所有退费任务完成情况成功，共 {Count} 条记录", result.Count());
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取所有退费任务完成情况失败");
                throw;
            }
        }

        /// <summary>
        /// 根据用户名获取退费任务完成情况
        /// </summary>
        /// <param name="userName">用户账号</param>
        /// <returns>退费任务完成情况列表</returns>
        public async Task<IEnumerable<RefundTaskCompletionStatus>> GetByUserNameAsync(string userName)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("用户账号不能为空", nameof(userName));
            }

            try
            {
                _logger.LogInformation("开始获取用户 {UserName} 的退费任务完成情况", userName);
                var result = await _repository.GetByUserNameAsync(userName);
                _logger.LogInformation("获取用户 {UserName} 的退费任务完成情况成功，共 {Count} 条记录", userName, result.Count());
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户 {UserName} 的退费任务完成情况失败", userName);
                throw;
            }
        }

        /// <summary>
        /// 根据用户姓名获取退费任务完成情况
        /// </summary>
        /// <param name="peopleName">用户姓名</param>
        /// <returns>退费任务完成情况列表</returns>
        public async Task<IEnumerable<RefundTaskCompletionStatus>> GetByPeopleNameAsync(string peopleName)
        {
            if (string.IsNullOrWhiteSpace(peopleName))
            {
                throw new ArgumentException("用户姓名不能为空", nameof(peopleName));
            }

            try
            {
                _logger.LogInformation("开始获取姓名包含 {PeopleName} 的退费任务完成情况", peopleName);
                var result = await _repository.GetByPeopleNameAsync(peopleName);
                _logger.LogInformation("获取姓名包含 {PeopleName} 的退费任务完成情况成功，共 {Count} 条记录", peopleName, result.Count());
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取姓名包含 {PeopleName} 的退费任务完成情况失败", peopleName);
                throw;
            }
        }

        /// <summary>
        /// 根据查询条件获取退费任务完成情况
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>退费任务完成情况列表</returns>
        public async Task<IEnumerable<RefundTaskCompletionStatus>> GetByConditionsAsync(RefundTaskCompletionStatusQueryRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request), "查询请求不能为空");
            }

            try
            {
                _logger.LogInformation("开始根据条件获取退费任务完成情况，用户账号: {UserName}, 用户姓名: {PeopleName}", 
                    request.UserName, request.PeopleName);
                var result = await _repository.GetByConditionsAsync(request);
                _logger.LogInformation("根据条件获取退费任务完成情况成功，共 {Count} 条记录", result.Count());
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "根据条件获取退费任务完成情况失败，用户账号: {UserName}, 用户姓名: {PeopleName}", 
                    request.UserName, request.PeopleName);
                throw;
            }
        }
    }
}